package edu.ccut.saturn.studio.export.excel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.eclipse.core.resources.IProject;

import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;

/**
 * 项目统计报告具体实现类
 * 
 * @author 杨艳斌
 * @author 施洪洁
 * 
 * @version 1.1 2009.12.17
 */
public class AnalyseModel {
	
	private static final String CLASSPATH_IS_TYPE_SRC = "src";
	private static final String SATURN_FOLDER_MODULE = "module";
	private static String projectname;
	private static HSSFWorkbook wb;
	private static int projectindex = 1;
	private static int projectNum;
	private static POIFSFileSystem fs;  
	private static FileOutputStream fileOut;
	private static int totalactionNums;
	private static int totallogicNums;
	private static int totalwfgNums;
	private static int totalcomponentNums; 
	private static int totaldataNums;
	private static int totalvalidateNums;
	private static int totalwebNums;
	private static int[] componentNums;
	private static String tempprojectname;
	private static List<HSSFSheet> clonesheets;
	
	/**
	 * 生成Excel格式的项目统计报告
	 * 
	 * @param project,savePath,projectNums,tempclonesheets
	 * @return
	 */
	public static void generateProjectSum(IProject project,
			String savePath, int projectNums, List<HSSFSheet> tempclonesheets) {
		
		projectNum = projectNums;
		int[] tempcomponetNums = new int[10];
		tempprojectname = project.getName();
		
		if (projectindex == 1) {
			projectname = project.getName();
		} else {
			projectname = projectname + "," + project.getName();
			if (projectname.length() > 100) {
				projectname = projectname + "\n";
			}
		}
		
		Set<String> srcs = SaturnProjectUtil.getClassPathsByType(project, CLASSPATH_IS_TYPE_SRC);
		
		//解析工程
		for (String src : srcs) {
			for (File fileMoudle : new File(src).listFiles()) {

				if (fileMoudle.isDirectory()) {
					if (SATURN_FOLDER_MODULE.equals(SaturnProjectUtil.saturnFileType(fileMoudle))) {

						for (File fileFloder : fileMoudle.listFiles()) {
							if (fileFloder.isDirectory()
									&& SaturnProjectUtil.isSaturnFloderType(SaturnProjectUtil.saturnFileType(fileFloder))) {

								try {
									tempcomponetNums = calcutlateComponents(fileFloder, tempcomponetNums);
								} catch (IOException e) {
									e.printStackTrace();
								}
							}
						}
					}
				}
			}
		}
		
		try {
			
			if (projectindex == 1) {
				
				fs  = new POIFSFileSystem(AnalyseModel.class.getResourceAsStream(	   //获得excel模板
							"/template/export/excel/saturn统计报告.xls"));   
				wb = new HSSFWorkbook(fs);											   //获得工作簿对象
				wb.setSheetName(1, tempprojectname);								   //命名第一个sheet
				fileOut = new FileOutputStream(savePath + "/" 						   //生成excel
			    		+ "saturn统计报告.xls");
				
				for (int i = 1; i <= projectNums - 1; i++) {							//sheet的复制
					HSSFSheet sheet = wb.cloneSheet(1);   
					tempclonesheets.add(sheet);
				}
				clonesheets = tempclonesheets;
			}
			
			componentNums = tempcomponetNums;
			generateExcel(savePath, wb, fileOut);										//生成excel
			
		} catch (IOException e) {
			e.printStackTrace();
		}  
	}
	
	/**
	 * 生成Excel格式的项目统计报告
	 * 
	 * @param savePath,wb,fileOut
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("deprecation")
	private static void generateExcel(String savePath, HSSFWorkbook wb, 
					FileOutputStream fileOut) throws IOException {
		
		 	HSSFRow row = null;
		 	HSSFRow comparerow = null;
		 	double totalnum = 0.0;
		 	
		 	for (int i = 0; i < componentNums.length ; i++) {
		 		totalnum = totalnum + (double)componentNums[i];
			}
		 	
			if (projectindex == 1) {
				 HSSFSheet sheet = wb.getSheetAt(projectindex); 
				 comparerow = sheet.getRow(3);
				 row = sheet.getRow(2); 
			} else {
				wb.setSheetName(projectindex, tempprojectname);
				row = clonesheets.get(projectindex - 2).getRow(2);
				comparerow =  clonesheets.get(projectindex - 2).getRow(3);
			}
		     
		    //acton构件统计
		    HSSFCell actioncell = row.getCell((short)1);   
		    if (actioncell == null)   
		    	actioncell = row.createCell((short)1);   
		    actioncell.setCellType(HSSFCell.CELL_TYPE_STRING);
		    actioncell.setCellValue(componentNums[0]);
		    
		    HSSFCell compareactioncell = comparerow.getCell((short)1);   
		    if (compareactioncell == null)   
		    	compareactioncell = row.createCell((short)1);   
		    compareactioncell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    compareactioncell.setCellValue(((double)Math.round
		    		(((double)componentNums[0]/totalnum)*100))/100);
		    
		    //logic构件统计 
		    HSSFCell logiccell = row.getCell((short)2);   
		    if (logiccell == null)   
		    	logiccell = row.createCell((short)2);   
		    logiccell.setCellType(HSSFCell.CELL_TYPE_STRING);
		    logiccell.setCellValue(componentNums[1]);
		    
		    HSSFCell comparelogiccell = comparerow.getCell((short)2);   
		    if (comparelogiccell == null)   
		    	comparelogiccell = row.createCell((short)2);   
		    comparelogiccell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    comparelogiccell.setCellValue(((double)Math.round
		    		(((double)componentNums[1]/totalnum)*100))/100);
		    
		    //component构件统计
		    HSSFCell componentcell = row.getCell((short)3);   
		    if (componentcell == null)   
		    	componentcell = row.createCell((short)3);   
		    componentcell.setCellType(HSSFCell.CELL_TYPE_STRING); 
		    componentcell.setCellValue(componentNums[2]);
		    
		    HSSFCell comparecomponentcell = comparerow.getCell((short)3);   
		    if (comparecomponentcell == null)   
		    	comparecomponentcell = row.createCell((short)3);   
		    comparecomponentcell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    comparecomponentcell.setCellValue(((double)Math.round
		    		(((double)componentNums[2]/totalnum)*100))/100);
		    
		    //wfg构件统计
		    HSSFCell wfgcell = row.getCell((short)4);   
		    if (wfgcell == null)   
		    	wfgcell = row.createCell((short)4);   
		    wfgcell.setCellType(HSSFCell.CELL_TYPE_STRING); 
		    wfgcell.setCellValue(componentNums[3]);
		    
		    HSSFCell comparewfgcell = comparerow.getCell((short)4);   
		    if (comparewfgcell == null)   
		    	comparewfgcell = row.createCell((short)4);   
		    comparewfgcell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    comparewfgcell.setCellValue(((double)Math.round
		    		(((double)componentNums[3]/totalnum)*100))/100);
		    
		    //validate构件统计
		    HSSFCell validatecell = row.getCell((short)5);   
		    if (validatecell == null)   
		    	validatecell = row.createCell((short)5);   
		    validatecell.setCellType(HSSFCell.CELL_TYPE_STRING);  
		    validatecell.setCellValue(componentNums[4]);
		    
		    HSSFCell comparevalidatecell = comparerow.getCell((short)5);   
		    if (comparevalidatecell == null)   
		    	comparevalidatecell = row.createCell((short)5);   
		    comparevalidatecell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    comparevalidatecell.setCellValue(((double)Math.round
		    		(((double)componentNums[4]/totalnum)*100))/100);
		    
		    //data构件统计
		    HSSFCell datacell = row.getCell((short)6);   
		    if (datacell == null)   
		    	datacell = row.createCell((short)6);   
		    datacell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    datacell.setCellValue(componentNums[5]);
		    
		    HSSFCell comparedatacell = comparerow.getCell((short)6);   
		    if (comparedatacell == null)   
		    	comparedatacell = row.createCell((short)6);   
		    comparedatacell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    comparedatacell.setCellValue(((double)Math.round
		    		(((double)componentNums[5]/totalnum)*100))/100);
		    
		    //web构件统计
		    HSSFCell webcell = row.getCell((short)7);   
		    if (webcell == null)   
		    	webcell = row.createCell((short)7);   
		    webcell.setCellType(HSSFCell.CELL_TYPE_STRING);
		    webcell.setCellValue(componentNums[6]);
		    
		    HSSFCell comparewebcell = comparerow.getCell((short)7);   
		    if (comparewebcell == null)   
		    	comparewebcell = row.createCell((short)7);   
		    comparewebcell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    comparewebcell.setCellValue(((double)Math.round
		    		(((double)componentNums[6]/totalnum)*100))/100);
		    
	    	 if (projectindex == projectNum) {
	    		 createSummry();												//创建项目总览sheet
	    		 wb.write(fileOut);														//往工作簿中写入统计数据
	    		 fileOut.close();														//关闭流
	    		 wb = null;
				 fs = null;
				 fileOut = null;
				 clonesheets = null;
				 projectindex = 0;
				 projectname = "";
				 totalactionNums = 0;
				 totallogicNums = 0;
				 totalwfgNums = 0;
				 totalcomponentNums = 0;
				 totaldataNums = 0;
				 totalvalidateNums = 0;
				 totalwebNums = 0;
	    	 }
	    	 projectindex++;
	}
	
	/**
	 * 生成Excel格式的项目统计报告中的项目总览sheet页
	 * 
	 * @return
	 */
	@SuppressWarnings("deprecation")
	private static void createSummry() {
		
		try {
			
		    HSSFSheet sheet = wb.getSheetAt(0);
		    HSSFRow row = sheet.getRow(3); 
		    HSSFRow comparerow = sheet.getRow(4);
		    HSSFRow sumrow = sheet.getRow(1);
		    double totalnum = (double)(totalactionNums + totalcomponentNums + totaldataNums 	//总的构件数
		    				+ totallogicNums + totalvalidateNums + totalwebNums + totalwfgNums);
		    
		    
		    //acton构件统计
		    HSSFCell actioncell = row.getCell((short)1);   
		    if (actioncell == null)   
		    	actioncell = row.createCell((short)1);   
		    actioncell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    actioncell.setCellValue(totalactionNums);
		    
		    HSSFCell compareactioncell = comparerow.getCell((short)1);   
		    if (compareactioncell == null)   
		    	compareactioncell = row.createCell((short)1);   
		    compareactioncell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    compareactioncell.setCellValue(((double)Math.round
		    		(((double)totalactionNums/totalnum)*100))/100);
		    
		    //logic构件统计 
		    HSSFCell logiccell = row.getCell((short)2);   
		    if (logiccell == null)   
		    	logiccell = row.createCell((short)2);   
		    logiccell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    logiccell.setCellValue(totallogicNums);
		    
		    HSSFCell comparelogiccell = comparerow.getCell((short)2);   
		    if (comparelogiccell == null)   
		    	comparelogiccell = row.createCell((short)2);   
		    comparelogiccell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    comparelogiccell.setCellValue(((double)Math.round
		    		(((double)totallogicNums/totalnum)*100))/100);
		    
		    //component构件统计
		    HSSFCell componentcell = row.getCell((short)3);   
		    if (componentcell == null)   
		    	componentcell = row.createCell((short)3);   
		    componentcell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    componentcell.setCellValue(totalcomponentNums);
		    
		    HSSFCell comparecomponentcell = comparerow.getCell((short)3);   
		    if (comparecomponentcell == null)   
		    	comparecomponentcell = row.createCell((short)3);   
		    comparecomponentcell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    comparecomponentcell.setCellValue(((double)Math.round
		    		(((double)totalcomponentNums/totalnum)*100))/100);
		    
		    //wfg构件统计
		    HSSFCell wfgcell = row.getCell((short)4);   
		    if (wfgcell == null)   
		    	wfgcell = row.createCell((short)4);   
		    wfgcell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    wfgcell.setCellValue(totalwfgNums);
		    
		    HSSFCell comparewfgcell = comparerow.getCell((short)4);   
		    if (comparewfgcell == null)   
		    	comparewfgcell = row.createCell((short)4);   
		    comparewfgcell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    comparewfgcell.setCellValue(((double)Math.round
		    		(((double)totalwfgNums/totalnum)*100))/100);
		    
		    //validate构件统计
		    HSSFCell validatecell = row.getCell((short)5);   
		    if (validatecell == null)   
		    	validatecell = row.createCell((short)5);   
		    validatecell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    validatecell.setCellValue(totalvalidateNums);
		    
		    HSSFCell comparevalidatecell = comparerow.getCell((short)5);   
		    if (comparevalidatecell == null)   
		    	comparevalidatecell = row.createCell((short)5);   
		    comparevalidatecell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    comparevalidatecell.setCellValue(((double)Math.round
		    		(((double)totalvalidateNums/totalnum)*100))/100);
		    
		    //data构件统计
		    HSSFCell datacell = row.getCell((short)6);   
		    if (datacell == null)   
		    	datacell = row.createCell((short)6);   
		    datacell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    datacell.setCellValue(totaldataNums);
		    
		    HSSFCell comparedatacell = comparerow.getCell((short)6);   
		    if (comparedatacell == null)   
		    	comparedatacell = row.createCell((short)6);   
		    comparedatacell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    comparedatacell.setCellValue(((double)Math.round
		    		(((double)totaldataNums/totalnum)*100))/100);
		    
		    //web构件统计
		    HSSFCell webcell = row.getCell((short)7);   
		    if (webcell == null)   
		    	webcell = row.createCell((short)7);   
		    webcell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    webcell.setCellValue(totalwebNums);
		    
		    HSSFCell comparewebcell = comparerow.getCell((short)7);   
		    if (comparewebcell == null)   
		    	comparewebcell = row.createCell((short)7);   
		    comparewebcell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    comparewebcell.setCellValue(((double)Math.round
		    		(((double)totalwebNums/totalnum)*100))/100);
		    
		    //项目简介
		    HSSFCell summerycell = sumrow.getCell((short)0);   
		    if (summerycell == null)   
		    	summerycell = sumrow.createCell((short)0);   
		    summerycell.setCellType(HSSFCell.CELL_TYPE_STRING);   
		    summerycell.setCellValue("该项目一共" + projectNum + 
		    		"个工程,工程名为" + projectname + ",共有如下构件：");
		}catch(Exception e) {   
		    e.printStackTrace();   
		}  
		
	}
	
	/**
	 * 统计各大构件的数量
	 * 
	 * @param fileFloder,tempcomponetNums
	 * @return int[]
	 * @throws IOException
	 */
	private static int[] calcutlateComponents(File fileFloder,
								int[] tempcomponetNums) throws IOException {

		String type = SaturnProjectUtil.saturnFileType(fileFloder);		
		int length = fileFloder.listFiles().length - 1;
			
		if ("action".equals(type)) {
			tempcomponetNums[0] = tempcomponetNums[0] + length;
			totalactionNums = totalactionNums + length;
		} else if ("logic".equals(type)) {
			tempcomponetNums[1] = tempcomponetNums[1] + length;
			totallogicNums = totallogicNums + length;
		} else if ("component".equals(type)) {
			tempcomponetNums[2] = tempcomponetNums[2] + length;
			totalcomponentNums = totalcomponentNums + length;
		} else if ("workflow".equals(type)) {
			tempcomponetNums[3] = tempcomponetNums[3] + length;
			totalwfgNums = totalwfgNums + length;
		} else if ("validate".equals(type)) {
			tempcomponetNums[4] = tempcomponetNums[4] + length;
			totalvalidateNums = totalvalidateNums + length;
		} else if ("data".equals(type)) {
			tempcomponetNums[5] = tempcomponetNums[5] + length;
			totaldataNums = totaldataNums + length;
		} else if ("web".equals(type)) {
			tempcomponetNums[6] =  tempcomponetNums[6] + length;
			totalwebNums = totalwebNums + length;
		}
		
		return tempcomponetNums;
		
	}
}
